package com.ry.novel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.service.BookService;
import com.ry.novel.model.dto.BookInfo;
import com.ry.novel.model.dto.condition.BookQuery;
import com.ry.novel.model.resp.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ry
 * @since 2022-03-27 17:13
 **/
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController extends BaseController {
    @Autowired
    private BookService bookService;

    @PostMapping("/searchByPage")
    public Result<Page<BookInfo>> searchByPage(@RequestBody BookQuery bookQuery) {
        log.info("bookQuery = "+bookQuery.getPage());

        return Result.ok(bookService.searchByPage(bookQuery));
    }

    @GetMapping("/queryAllBook")
    public Result<List<BookInfo>> queryAllBook(){
        List<BookInfo> bookInfos = bookService.queryAll();

        return Result.ok(bookInfos);
    }

    @PostMapping("/addBook")
    public Result<Void> addBook(@RequestBody BookInfo bookInfo){
        bookService.addBook(bookInfo);

        return Result.ok();
    }

    @PostMapping("/updateBook")
    public Result<Void> updateBook(@RequestBody BookInfo bookInfo){
        bookService.updateBook(bookInfo);

        return Result.ok();
    }

    @PostMapping("/deleteBook")
    public Result<Void> deleteBook(@RequestParam("id") Long id){
        bookService.deleteBook(id);

        return Result.ok();
    }
}